//
//  Identify.m
//  SecretPhoto
//
//  Created by administrator on 12-9-13.
//  Copyright (c) 2012年 com.ptc. All rights reserved.
//

#import "Identify.h"

@implementation Identify

@synthesize leftEye;
@synthesize rightEye;
@synthesize mouth;

@synthesize temp_rightEye;
@synthesize temp_leftEye;
@synthesize temp_mouth;

-(void)readDataFromPlist
{
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    plistPath = [rootPath stringByAppendingPathComponent:@"tempData.plist"];
    
    NSArray *tempArray =[NSArray arrayWithContentsOfFile:plistPath];
    
    temp_leftEye.x =[[tempArray objectAtIndex:0] floatValue];
    temp_leftEye.y =[[tempArray objectAtIndex:1] floatValue];
    temp_rightEye.x =[[tempArray objectAtIndex:2] floatValue];
    temp_rightEye.y =[[tempArray objectAtIndex:3] floatValue];
    temp_mouth.x =[[tempArray objectAtIndex:4] floatValue];
    temp_mouth.y =[[tempArray objectAtIndex:5] floatValue];
    
    NSLog(@"%f", temp_leftEye.x);
}

-(int)checkWithLeftEye
{
    
    
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    
    plistPath = [rootPath stringByAppendingPathComponent:@"ownerData.plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        return 2;
    }
    NSArray *Array =[NSArray arrayWithContentsOfFile:plistPath];
    leftEye.x =[[Array objectAtIndex:0] floatValue];
    leftEye.y =[[Array objectAtIndex:1] floatValue];
    rightEye.x =[[Array objectAtIndex:2] floatValue];
    rightEye.y =[[Array objectAtIndex:3] floatValue];
    mouth.x =[[Array objectAtIndex:4] floatValue];
    mouth.y =[[Array objectAtIndex:5] floatValue];
    
    float eyeDistance =[self eyeDistanceAx:leftEye.x Ay:rightEye.y Bx:leftEye.x By:rightEye.y];
    float tempEyeDistance =[self eyeDistanceAx:temp_leftEye.x Ay:temp_leftEye.y Bx:temp_rightEye.x By:temp_rightEye.y];
    
    if (eyeDistance*0.95< tempEyeDistance &&
        eyeDistance*1.05>tempEyeDistance) {
//        UIAlertView *a =[[[UIAlertView alloc]initWithTitle:@"success!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
//        [a show];
        return 0;
    } else {
//        UIAlertView *a =[[[UIAlertView alloc]initWithTitle:@"failed!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
//        [a show];
        return 1;
    }
}

-(float)eyeDistanceAx:(float)ax Ay:(float)ay Bx:(float)bx By:(float)by
{
    float dx =bx -ax;
    float dy =by -ay;
    
    float d =sqrt(dx*dx+dy*dy);
    
    return d;
}

-(void)saveDataToPlist
{
    
}


@end
